
#include "stdafx.h"
#include "tmediatypehelper.h"
#include <streams.h>
#include "utility/asyncio.h"
#include "utility/asyncrdr.h"

namespace TMeadiaTypeHelper
{

// {59fcbec7-524f-11ce-9f53-0020af0ba770}
GUID MEDIASUBTYPE_m4v =
{ 
    0x59fcbec7, 0x524f, 0x11ce, { 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70 } 
};

// C9361F5A-3282-4944-9899-6D99CDC5370B
GUID Guid_Ogg =
{
    0xC9361F5A, 0x3282, 0x4944, { 0x98, 0x99, 0x6D, 0x99, 0xCD, 0xC5, 0x37, 0x0B }
};

// ED3110F8-5211-11DF-94AF-0026B977EEAA
GUID Guid_WebmSplitterFilterClass =
{
    0xED3110F8, 0x5211, 0x11DF, { 0x94, 0xAF, 0x00, 0x26, 0xB9, 0x77, 0xEE, 0xAA }
};

// ED3110EB-5211-11DF-94AF-0026B977EEAA 
GUID Guid_Webm =
{
    0xED3110EB, 0x5211, 0x11DF, { 0x94, 0xAF, 0x00, 0x26, 0xB9, 0x77, 0xEE, 0xAA }
};

GUID Guid_Flv =
{ 
    0xD27CDB6E, 0xAE6D, 0x11CF, { 0x96, 0xB8, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00 } 
};

bool IsMp3MediaHead(const MprMediaHead pHead)
{
    return pHead[0] == 'I' && pHead[1] == 'D' && pHead[2] == '3';
}

bool IsOggMediaHead(const MprMediaHead pHead)
{
    return pHead[0] == 'O' && pHead[1] == 'g' && pHead[2] == 'g' && pHead[3] == 'S';
}

bool IsAviMediaHead(const MprMediaHead pHead)
{
    return pHead[0] == 'R' && pHead[1] == 'I' && pHead[2] == 'F' && pHead[3] == 'F' && 
        pHead[8] == 'A' && pHead[9] == 'V' && pHead[10] == 'I';
}

bool IsWebmMediaHead(const MprMediaHead pHead)
{
    return pHead[24] == 'w' && pHead[25] == 'e' && pHead[26] == 'b' && pHead[27] == 'm';
}

bool IsMp4MediaHead(const MprMediaHead pHead)
{
    return pHead[4] == 'f' && pHead[5] == 't' && pHead[6] == 'y' && pHead[7] == 'p';
}

bool IsSwfMediaHead(const MprMediaHead pHead)
{
    return (pHead[0] == 'F' && pHead[1] == 'W' && pHead[2] == 'S')||(pHead[0] == 'S' && pHead[1] == 'W' && pHead[2] == 'F')||(pHead[0] == 'C' && pHead[1] == 'W' && pHead[2] == 'S');
}

bool GetMediaType(MprMediaHead head, MprMediaType &mt, GUID &Guid)
{
    if (IsOggMediaHead(head))
    {
        mt = MMT_Audio;
        Guid = Guid_Ogg;
    }
    else if (IsWebmMediaHead(head))
    {
        mt = MMT_Video;
        Guid = Guid_Webm;
    }
    else if (IsMp3MediaHead(head))
    {
        mt = MMT_Audio;
        Guid = Guid_Mp3;
    }
    else if (IsAviMediaHead(head))
    {
        mt = MMT_Video;
        Guid = Guid_Avi;
    }
    else if (IsSwfMediaHead(head))
    {
        mt = MMT_Flash;
        Guid = Guid_Flv;
    }
    else if (IsMp4MediaHead(head))
    {
        mt = MMT_Video;
        Guid = Guid_Mp4;
    }
    else
    {
        return false;
    }
    return true;
}
};